package com.peachcastle.core 
{
	import com.peachcastle.events.LoaderEvent;
	import com.peachcastle.net.QueueLoader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import com.peachcastle.interfaces.IScene;

	public class Scene extends EventDispatcher implements IScene
	{
		
		private var _resource:Array = null;
		
		private var _name:String = "Scene";
		
		private var _isLoading:Boolean = false;//是否在加载中
		
		private var _isShowLoading:Boolean = true;//是否显示Loading
		
		private var _isLoadcomplete:Boolean = false;//是否加载完成;
		
		private var _queueLoader:QueueLoader = null;//队列加载;
		
		private var _initialization:Boolean = false;//是否完成了初始化;
		
		protected var _assest:MovieClip = null;
		
		private var _isinitialization:Boolean = false;
		
		private var _swfUrlFunc:Function = null;
		
		public function Scene(n:String = "", swfUrlFunc:Function = null, rec:Array = null) 
		{
			_name = n, _swfUrlFunc = swfUrlFunc, _resource = rec;
		}
		
		/**
		 * 预加载
		 */
		public function preLoader():void
		{
			if (_isLoadcomplete || _isLoading || (_resource && _resource.length < 1)) return;
			_isShowLoading = false;		//预加载，不显示LOADING的动画
			loaderRec();
		}
		
		protected function loaderRec():void
		{
			_isLoading = true;
			if (!_queueLoader) _queueLoader = new QueueLoader();
			_queueLoader.addEventListener(LoaderEvent.LOADERALL_COMPLETE, onAllComplete);
			_queueLoader.addEventListener(LoaderEvent.LOADER_PROGRESS, onProgress);
			for each(var item:String in resource)
			{
				_queueLoader.addItem( _swfUrlFunc != null ? _swfUrlFunc(item) : item, item);
			}
			_queueLoader.start();
			
		}
		
		protected function onProgress(event:LoaderEvent):void
		{
			if (!_isShowLoading) return;
			showLoading(event.progress);
		}
		
		private function onAllComplete(event:LoaderEvent):void
		{
			_queueLoader.removeEventListener(LoaderEvent.LOADERALL_COMPLETE, onAllComplete);
			_queueLoader.removeEventListener(LoaderEvent.LOADER_PROGRESS, onProgress);
			
			_isLoading = false;
			_isLoadcomplete = true;
			if (_isShowLoading)
			{
				enterScene();
			}
		}
		
		/**
		 * 显示百分比;
		 * @param	percent
		 */
		protected function showLoading(percent:Number = 0):void
		{
			//Printer.print("显示为:" + _isShowLoading, "当前进度:" + percent);
		}
		
		protected function initializationView():void
		{
			
		}
		protected function configureEventListeners():void
		{
			
		}
		
		public function leaveScene():void
		{
			_initialization = false;
			
			removeEventListeners();
			removeLayer();
			dispose();
		}
		
		protected function removeLayer():void
		{
			
		}
		
		protected function removeEventListeners():void
		{
			
		}
		
		protected function dispose():void
		{
			
		}
		
		public function joinScene():void
		{
			if (_isLoadcomplete) 
			{
				enterScene();
				return;
			}
			if (!_isLoadcomplete)
			{
				_isShowLoading = true;
				if (!_isLoading)
				{
					loaderRec();
				}
				return;
			}
		}
		
		
		/**
		 * 进入场景;
		 */
		protected function enterScene():void
		{
			if (!_initialization)
			{
				_initialization = true;
				initializationView();
				configureEventListeners();
			}
		}
		
		public function addLayer():void
		{

		}
		
		/**
		 * 类库查找类
		 * @param	lib
		 * @param	name
		 * @return
		 */
		protected function getLibByClass(lib:String, name:String):Class
		{
			if (ApplicationDomain.currentDomain.hasDefinition(name))
			{
				return ApplicationDomain.currentDomain.getDefinition(name) as Class;
			}
			
			if (!_queueLoader.getNameByLibary(lib).applicate.hasDefinition(name)) return null;
			
			return _queueLoader.getNameByLibary(lib).applicate.getDefinition(name) as Class;
		}
		
		public function getRecObject(lib:String,className:String):MovieClip
		{
			return null;
		}
		
		/**
		 * 场景名称;
		 */
		public function get name():String { return _name; }
		
		/**
		 * 资源列表;
		 */
		public function get resource():Array { return _resource; }
		
		/**
		 * 层;
		 */
		public function get layer():Sprite { return null; }
		
		public function get assest():MovieClip { return _assest; }
		
	}

}